aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/[username]/dashboard/+page.svelte
blob: 160887e37cd52b966b99f07c667cada8c32c0864 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script>
	import { f, s, post, failure, sats } from '$lib/utils';
	import { tick } from 'svelte';
	import { Icon } from '$comp';
	import { rate, selectedRate } from '$lib/store';
	import { goto } from '$app/navigation';
	import { t } from '$lib/translations';

	export let data;

	let ease = (t) => (t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t);
	let interval;

	$: animateRate($selectedRate);
	let animateRate = (newRate) => {
		if (!accountBalanceFiat) return;
		let t = 0;
		let newBalance = (user.account.balance * newRate) / sats;
		let oldBalance = parseFloat(accountBalanceFiat);
		let diff = oldBalance - newBalance;

		clearInterval(interval);
		interval = setInterval(() => {
			let delta = diff * ease(t / 100);
			accountBalanceFiat = (oldBalance - delta).toFixed(2);
			if (t > 80) clearInterval(interval);
			t++;
		}, 10);
	};

	$: user = data.user;
	$: accountBalanceFiat =
		accountBalanceFiat ||
		(user && $selectedRate && ((user.account.balance * $selectedRate) / sats).toFixed(2));

	let payreq = '',
		payreqField,
		loading;

	$: btcPrice = f($selectedRate, user.currency);

	$: accountBalanceBtc = user.account.balance / sats;

	$: accountBalanceSats = s(user.account.balance);
</script>

<div class="container px-4 max-w-md mx-auto mt-20">
	<div class="text-5xl font-bold mb-2">
		{$selectedRate ? f(accountBalanceFiat, user.currency) : $t('user.dashboard.fetchingRate')}
	</div>

	<div class="text-secondary text-2xl">
		{accountBalanceSats} SAT
	</div>

	<div class="grid sm:grid-cols-2 gap-2 mt-8">
		<a href={`/${user.username}/receive`}>
			<button class="rounded-full border py-2 px-5 font-bold hover:opacity-80 w-full mb-2"
				>{$t('user.dashboard.receive')}</button
			>
		</a>
		{#if user.account.balance > 0}
			<a href="/send">
				<button class="rounded-full border py-2 px-5 font-bold hover:opacity-80 w-full mb-2"
					>{$t('user.dashboard.withdraw')}</button
				>
			</a>
		{:else}
			<a href={`/${user.username}/settings`}>
				<button class="rounded-full border py-2 px-5 font-bold hover:opacity-80 w-full mb-2"
					>Setup Account</button
				>
			</a>
		{/if}
	</div>
</div>